/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.manage.service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheKit;

import cn.hg.jfsa.model.GoodsCategory;
import cn.hg.jfsa.model.GoodsType;
import cn.hg.jfsa.web.theme.ThemeManager;

/**
 * @author Golly
 */
public class GoodsService {

	/**
	 * @param bean
	 */
	public void delGoodsCategory (GoodsCategory bean) {
		this.delGoodsCategory(bean, true);
	}

	/**
	 * @param bean
	 * @param withChildren
	 */
	public void delGoodsCategory (GoodsCategory bean, boolean withChildren) {

		if (withChildren) {
			this.delGoodsCategoryWithChildren(bean);
		} else {
			bean.setStatus("del").update();
		}

	}

	public List<GoodsCategory> getGoodsCategoryAll () {
		return GoodsCategory.DAO.find("select * from t_goods_category order by sort_num");
	}

	public List<GoodsCategory> getGoodsCategoryTree () {
		List<GoodsCategory> tree = CacheKit.get("goodsCategory", "tree");

		if (null == tree) {
			List<GoodsCategory> list = GoodsCategory.DAO.find("SELECT * FROM t_goods_category WHERE status=?", "on");
			tree = this.buildGoodsCategoryTree(list, 0);
			CacheKit.put("goodsCategory", "tree", tree);
		}

		return tree;
	}

	public List<GoodsType> getGoodsTypeByAttrCategory () {
		Kv para = Kv.by("theme", ThemeManager.me().getCurrentTheme().getId());
		SqlPara sqlPara = GoodsType.DAO.getSqlPara("module.findGoodsTypeByAttrCategory", para);
		return GoodsType.DAO.find(sqlPara);
	}

	/**
	 * @param org
	 */
	public void updateGoodsCategoryChildredIds (GoodsCategory org) {

		if (null == org) {
			return;
		}

		String childrenIds =
			Db.queryFirst("SELECT GROUP_CONCAT(id) FROM t_goods_category WHERE status<>'del' AND parent_id=?",
				org.getId());
		org.setChildrenIds(childrenIds).update();
	}

	/**
	 * @param list
	 * @param parentId
	 *
	 * @return
	 */
	private List<GoodsCategory> buildGoodsCategoryTree (final List<GoodsCategory> list, Integer parentId) {
		List<GoodsCategory> tree = new ArrayList<>();

		for (GoodsCategory org : list) {

			if (parentId.equals(org.getParentId())) {
				tree.add(org);
			}

		}

		for (GoodsCategory org : tree) {
			org.setChildren(this.buildGoodsCategoryTree(list, org.getId()));
		}

		tree.sort(new Comparator<GoodsCategory>() {

			@Override
			public int compare (GoodsCategory o1, GoodsCategory o2) {
				return o1.getSortNum().compareTo(o2.getSortNum());
			}

		});
		return tree;
	}

	/**
	 * @param bean
	 */
	private void delGoodsCategoryWithChildren (GoodsCategory bean) {
		bean.setStatus("del").update();
		List<GoodsCategory> childrenList =
			GoodsCategory.DAO.find("SELECT * FROM t_goods_category WHERE status<>'del' AND parent_id = ?",
				bean.getId());

		for (GoodsCategory child : childrenList) {
			child.setStatus("del").update();
			this.delGoodsCategoryWithChildren(child);
		}

	}

}
